package com.mmlc.meimei.lichengkongjian.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.androidnetframe.BaseParentActivity;
import com.mmlc.businessutils.OrderListUtils;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.utils.SelectorUtils;

import java.util.List;

public class MuchButtonGroup extends RadioGroup implements View.OnClickListener
{

	public BaseParentActivity activity;

	public MuchButtonGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(RadioGroup.HORIZONTAL);
	}
	/**
	 * 根据订单button的类型，自动的生成相应的n个button
	 * */
	public void addButton(List<Integer> orderButtonType){
		this.removeAllViews();
		for(int i=0;i<orderButtonType.size();i++){
			LinearLayout ll_view = (LinearLayout) View.inflate(getContext(), R.layout.view_muchbuttongroup, null);
			RadioButton radioButton = (RadioButton) ll_view.getChildAt(0);
			radioButton.setBackgroundDrawable(SelectorUtils.getSelector(SelectorUtils.shape_GrayStroke_Gray));
			addView(ll_view);

			int tmp = orderButtonType.get(i);
			initView(tmp,radioButton);
		}
	}

	CustomShowListView.ViewConfig config;
	public void addButton(List<Integer> orderButtonType, final CustomShowListView.ViewConfig config){
		this.removeAllViews();
		for(int i=0;i<orderButtonType.size();i++){
			LinearLayout ll_view = (LinearLayout) View.inflate(getContext(), R.layout.view_muchbuttongroup, null);
			RadioButton radioButton = (RadioButton) ll_view.getChildAt(0);
			addView(ll_view);
			ll_view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Integer tag = (Integer) v.getTag();
					if(config != null){
						config.buttonClicked(v,tag);
					}
				}
			});

			int tmp = orderButtonType.get(i);
			initView1(tmp,radioButton);
		}
	}
	private void initView(int tmp, RadioButton view) {
		view.setOnClickListener(this);
		String[] value = OrderListUtils.getStringFromType(tmp);
		String text = value[0];
		int tag = Integer.valueOf(value[1]);
		view.setText(text);
		view.setTag(tag);
	}
	private void initView1(int tmp, RadioButton view) {
		String[] value = OrderListUtils.getStringFromType(tmp);
		String text = value[0];
		int tag = Integer.valueOf(value[1]);
		view.setText(text);
		view.setTag(tag);
	}
	@Override
	public void onClick(View v) {
		Integer tag = (Integer) v.getTag();
		if(listener != null){
			listener.clicked(v,tag);
		}
	}

	public interface MuchButtonClickedListener{
		public void clicked(View v, int type);
	}
	MuchButtonClickedListener listener;
	public void setMuchButtonClickedListener(MuchButtonClickedListener listener){
		this.listener = listener;
	}
}
